﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow.Filters;
using System.Threading;

namespace System.Windows.Media.DirectShow.Avi
{
    internal class AviSourceSplitterAudioOutputPin : BaseOutputPin
    {

        private AMMediaType[] _mediaTypes;
        private WaveFormatExtensible format;

        public AviSourceSplitterAudioOutputPin(AviSourceSplitter filter, AMMediaType mediaType)
            : base(filter)
        {
            _mediaTypes = new AMMediaType[] { mediaType };
        }

        protected override void Connect(IPin receivePin, AMMediaType mediaType)
        {
            base.Connect(receivePin, mediaType);

            format = (WaveFormatExtensible)this.ConnectedMediaType.Format;
        }

        protected internal override void DeliverAsync(IMediaSample sample)
        {
            if (_mediaTypes[0].SubType == MediaSubTypes.PCM)
            {
                TimeSpan start = TimeSpan.FromSeconds(sample.StartMediaTime / (double)format.AverageBytesPerSecond);
                TimeSpan end = TimeSpan.FromSeconds(sample.EndMediaTime / (double)format.AverageBytesPerSecond);

                // Set sample parameters
                sample.StartTime = start;
                sample.EndTime = end;
            }

            sample.MediaType = this._mediaTypes[0];

            base.DeliverAsync(sample);
        }

        public override System.Collections.Generic.IEnumerable<AMMediaType> MediaTypes
        {
            get { return _mediaTypes; }
        }

        public override string Name
        {
            get { return "Audio output"; }
        }

    }
}
